package com.xkcun.bi.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcun.bi.entity.City;
import com.xkcun.bi.model.Report;
import com.xkcun.bi.param.*;
import com.xkcun.bi.service.CityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 市(City)表控制层
 *
 * @author yfz
 * @since 2021-08-06 15:22:45
 */
@Api(value = "", description = "")
@RestController
@RequestMapping("city")
public class CityApi extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CityService cityService;



    @ApiOperation(value = "查找城市报表", notes = "查找城市报表")
    @PostMapping("/reports")
    public List<Report> reports(@RequestParam List<Integer> ids) {
        List<City> list = cityService.listByIds(ids);
        List<Report> reports = new ArrayList<>();
        list.forEach(item->{
            Report report = new Report();
            report.setNama(item.getValue());
            report.setCode(item.getCode());
            reports.add(report);
        });
        return reports;
    }


    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page")
    public R page(CityListParam param) {
        Page<City> page = new Page(param.getPageIndex(), param.getPageSize());
        City city = new City();
        BeanUtils.copyProperties(param, city);
        return success(this.cityService.page(page, new QueryWrapper<>(city)));
    }

    @ApiOperation(value = "获取所有数据", notes = "获取所有数据")
    @PostMapping("/selectAll")
    public R selectAll() {
        return success(this.cityService.list());
    }

    @ApiOperation(value = "详情查询", notes = "详情查询")
    @GetMapping("{id}")
    public R detail(@PathVariable Integer id) {
        return success(this.cityService.getById(id));
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public R insert(@RequestBody City city) {
        return success(this.cityService.save(city));
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public R update(@RequestBody City city) {
        return success(this.cityService.updateById(city));
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/del/{id}")
    public R delete(@PathVariable Integer id) {
        return success(this.cityService.removeById(id));
    }
}